import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue"
import { objectAny } from "../../../options/base";
import { mySchoolStore } from "../../../store";
import {  getMySelf, getUserAddressList } from "../../../utils/doCloud";
import { goToBack, showModal, showToast } from "../../../utils/utils";

const clientUserManage = uniCloud.importObject(`client-user-manage`, {customUI: true});

const db = uniCloud.database()

export default {
    setup() {
        const loading = ref(false);
        const schoolStore = mySchoolStore();
        const editType = ref<'edit' | 'add'>('add');
        const formData = ref({
            name: ``,
            moblie: ``,
            address: ``,
            is_default: false,
            school_id: schoolStore.mySchool._id,
            user_id: ``,
            _id: ``
        })
        getMySelf().then(res => {
            formData.value.user_id = res._id
        })
        onLoad(async (options) => {
            const toggleTitle = {
                edit: `编辑收件人地址`,
                add: `新增收件人地址`
            }
            const type = options[`type`] as (`edit` | `add`);
            editType.value = type
            if (type) {
                const title = toggleTitle[type];
                uni.setNavigationBarTitle({
                    title
                });
            }
            if (options.id && type == `edit`) {
                const {myErrCode,data:addressList} = await getUserAddressList(schoolStore.mySchool._id,options.id);
                if(addressList){
                    formData.value=addressList[0]
                }
            }
            loading.value = false;
        })

        const save = async () => {
            const _deFormData = formData.value
            if (_deFormData.name.trim() === ``) {
                return showToast(`收件人不能为空`, 'none')
            }
            if (_deFormData.moblie.trim() === ``) {
                return showToast(`电话号码不能为空`, 'none')
            }
            if (_deFormData.address.trim() === ``) {
                return showToast(`详细地址不能为空`, 'none')
            }
            if (!/^1[3-9]\d{9}$/.test(_deFormData.moblie.trim())) {
                return showToast(`手机号码格式不正确`, 'none')
            }
            const { _id, ...doData } = formData.value

            uni.showLoading({ title: `正在处理`, mask: true })

            try {
                let result=null;
                if (editType.value === 'add') {
                    result =await clientUserManage.saveUserAddress(doData);
                } else {
                    result=await clientUserManage.updateUserAddress(_id,doData);
                }

                const {myErrCode,data,errMsg}=result
                if(myErrCode!==0){
                    return showModal({
                        content:errMsg,
                        title:`异常提示`,
                        showCancel:false
                    })
                }
                showToast(data.msg);
                return goToBack();
            } catch (error) {
                console.log(error)
                return showModal({
                    content:`系统繁忙请稍后再试`,
                    title:`异常提示`,
                    showCancel:false
                })
            }
        }

        return {
            editType,
            loading,
            formData,
            schoolStore,
            save
        }
    }
}